import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-10-14
 * Time:13:47
 */
public class Test {
    public static void main(String[] args) {
        //LinkedList 的遍历
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.addLast(1);
        linkedList.addLast(2);
        linkedList.addLast(3);
        linkedList.addLast(4);
        linkedList.addLast(5);
        System.out.println("===直接sout打印===");
        System.out.println(linkedList);

        System.out.println("===for循环遍历===");
        for (int i = 0;i < linkedList.size(); i++) {
            System.out.print(linkedList.get(i) + " ");
        }
        System.out.println();

        System.out.println("===for-each遍历===");
        for(Integer x : linkedList) {
            System.out.print(x + " ");
        }
        System.out.println();

        System.out.println("===使用iterator迭代器遍历===");
        Iterator<Integer> it = linkedList.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        System.out.println();

        System.out.println("===使用listIterator迭代器遍历——正向遍历===");
        ListIterator<Integer> lit = linkedList.listIterator(0);
        while (lit.hasNext()) {
            System.out.print(lit.next() + " ");
        }
        System.out.println();

        System.out.println("===使用listIterator迭代器遍历——反向遍历===");
        ListIterator<Integer> lit1 = linkedList.listIterator(linkedList.size());
        while (lit1.hasPrevious()) {
            System.out.print(lit1.previous() + " ");
        }
        System.out.println();
    }
    public static void main7(String[] args) {
        //LinkedList类的其他常用方法
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(1);//add默认都是尾插
        linkedList.addFirst(2);
        linkedList.addLast(3);
        linkedList.addLast(4);
        linkedList.addLast(5);
        System.out.println(linkedList);//[2, 1, 3, 4, 5]

        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(11);
        arrayList.add(12);
        arrayList.add(13);

        linkedList.addAll(arrayList);//将实现了Collection类的引用内容尾插到LinkedList中
        System.out.println(linkedList);//[2, 1, 3, 4, 5, 11, 12, 13]
    }
    public static void main6(String[] args) {
        //构造一个空的LinkedList（无参构造方法）
        List<Integer> list1 = new LinkedList<>();

        List<String> list2 = new ArrayList<>();
        list2.add("hello");
        list2.add("abcd");
        list2.add("world");
        //使用ArrayList构造LinkedList
        List<String> list = new LinkedList<>(list2);
        System.out.println(list);
    }
    public static void main5(String[] args) {
        //构造一个空的LinkedList（无参构造方法）
        LinkedList<Integer> list = new LinkedList<>();

        LinkedList<String> list1 = new LinkedList<>();
        list1.add("hello");//add默认都是尾插
        list1.add("abcd");
        list1.add("school");
        //使用list1构造list2
        LinkedList<String> list2 = new LinkedList<>(list1);
        System.out.println(list2);
    }
    public static void main4(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.addLast(5);

        list.display();
        System.out.println("======");
        list.clear();
        list.display();
    }
    public static void main3(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addLast(1);
        list.addLast(1);
        list.addLast(5);
        list.addLast(1);
        list.addLast(1);
        list.display();

        list.removeAllKey(1);
        list.display();
    }
    public static void main2(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.addLast(5);

        list.display();//1 2 3 4 5

        list.addIndex(0,9);
        list.addIndex(3,99);
        list.addIndex(list.size(), 999);

        list.display();//9 1 2 99 3 4 5 999

        list.remove(9);
        list.remove(4);
        list.remove(999);
        list.display();//1 2 99 3 5

        System.out.println("===============");
        MyLinkedList list1 = new MyLinkedList();
        list1.addLast(1);
        list1.remove(1);
        list1.display();
    }
    public static void main1(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addFirst(1);
        list.addFirst(2);
        list.addFirst(3);
        list.addFirst(4);
        list.addFirst(5);

        list.display();//5 4 3 2 1
        int size = list.size();
        System.out.println(size);
        System.out.println(list.contains(4));
    }
}
